package com.xjj.order.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.AbstractPathProvider;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @description: Swagger配置文件
 * @author: zhanghejie
 * @create: 2020-09-29
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现，
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     *
     * @return
     */

    @Value("${swagger.show:true}")
    private boolean swaggerShow;

    @Bean
    public Docket createRestApi() {

        String ip="";
        String prefix="";
        try {
            InetAddress address = InetAddress.getLocalHost();
            ip=address.getHostAddress();
        } catch (UnknownHostException e) {

        }
//        if(ip.indexOf("8.131.64.29")>-1||ip.indexOf("192.168")>-1) {
//            prefix="";
//        }else {
//            prefix="/manage";
//        }
//        ParameterBuilder ticketPar = new ParameterBuilder();
//        List<Parameter> pars = new ArrayList<Parameter>();
//        ticketPar.name("Authorization").description("认证token")
//                .modelRef(new ModelRef("string")).parameterType("header").defaultValue("")
//                .required(true).build();
//        pars.add(ticketPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xjj.order"))
                .paths(PathSelectors.any())
                .build().pathProvider(new CustRelativePathProvider(prefix));
    }

    /**
     * 创建该API的基本信息（这些基本信息会展现在文档页面中）
     * 访问地址：http://项目实际地址/swagger-ui.html
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("订单服务api")
                .description("请关注jeexjj系列开源项目")
                .termsOfServiceUrl("https://gitee.com/zhanghejie")
                .version("2.0")
                .build();
    }

    public class CustRelativePathProvider extends AbstractPathProvider {
        public static final String ROOT = "/";
        public String prefix="";
        public CustRelativePathProvider(String prefix) {
            super();
            this.prefix=prefix;
        }

        @Override
        public String getOperationPath(String operationPath) {
            return prefix+operationPath;
        }

        @Override
        protected String applicationPath() {
            return ROOT;
        }

        @Override
        protected String getDocumentationPath() {
            return ROOT;
        }
    }


}
